using System.Configuration;

namespace IFXplus.SmartStorage.Settings
{
    public class StorageSettings : ConfigurationSection
    {
        
        #region GetInstance (singleton)

        // singleton
        private static volatile StorageSettings _instance = null;
        private static readonly object _syncRoot = new object();
        
        private StorageSettings() { }

        public static StorageSettings GetInstance()
        {
            if (_instance == null)
            {
                lock (_syncRoot)
                {
                    if (_instance == null)
                        _instance = ConfigurationManager.GetSection("storageSettings") as StorageSettings;
                }
            }
            return _instance;
        }

        #endregion

        #region Shared (const)

        public static readonly int MAX_KEY_LENGTH = 1000;

        #endregion

        #region StorageProviders

        [ConfigurationProperty("AspNetCacheStorageProviders")]
        private StorageProviderSettingsCollection<AspNetCacheStorageProviderSettings> AspNetCacheStorageProviders
        {
            get { return ((StorageProviderSettingsCollection<AspNetCacheStorageProviderSettings>)(base["AspNetCacheStorageProviders"])); }
        }

        [ConfigurationProperty("MemoryStorageProviders")]
        private StorageProviderSettingsCollection<MemoryStorageProviderSettings> MemoryStorageProviders
        {
            get { return ((StorageProviderSettingsCollection<MemoryStorageProviderSettings>)(base["MemoryStorageProviders"])); }
        }

        [ConfigurationProperty("FileSystemStorageProviders")]
        private StorageProviderSettingsCollection<FileSystemStorageProviderSettings> FileSystemStorageProviders
        {
            get { return ((StorageProviderSettingsCollection<FileSystemStorageProviderSettings>)(base["FileSystemStorageProviders"])); }
        }

        [ConfigurationProperty("SqlServerStorageProviders")]
        private StorageProviderSettingsCollection<SqlServerStorageProviderSettings> SqlServerStorageProviders
        {
            get { return ((StorageProviderSettingsCollection<SqlServerStorageProviderSettings>)(base["SqlServerStorageProviders"])); }
        }

        [ConfigurationProperty("WebServiceStorageProviders")]
        private StorageProviderSettingsCollection<WebServiceStorageProviderSettings> WebServiceStorageProviders
        {
            get { return ((StorageProviderSettingsCollection<WebServiceStorageProviderSettings>)(base["WebServiceStorageProviders"])); }
        }
        
        #endregion

        #region GetStorageProviderSettings

        public T GetStorageProviderSettings<T>(string id) where T : class, IStorageProviderSettings, new()
        {
            T ps = new T();
            switch(ps.Type)
            {
                
                case StorageProviderType.AspNetCache:
                    return AspNetCacheStorageProviders[id] as T;

                case StorageProviderType.Memory:
                    return MemoryStorageProviders[id] as T;

                case StorageProviderType.FileSystem:
                    return FileSystemStorageProviders[id] as T;

                case StorageProviderType.SqlServer:
                    return SqlServerStorageProviders[id] as T;

                case StorageProviderType.WebService:
                    return WebServiceStorageProviders[id] as T;

                default:
                    throw new StorageSettingsException(string.Format("Impossibile trovare la configurazione per il provider '{0}'", ps.Type));
            }
        }

        #endregion

    }
}
